package csrb;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

public class Profile implements Serializable {

	private static final long serialVersionUID = 4866595259897229283L;

	public static final DateFormat DATE_FORMAT = new SimpleDateFormat(
			"MMM dd yyyy", Locale.US);

	public static final NumberFormat NUMBER_FORMAT = new DecimalFormat(
			"###,###");

	public static final NumberFormat FLOAT_FORMAT = new DecimalFormat("#0.0#");

	private String callsign;

	private String rank;

	private Date enlisted;

	private Side side;

	private Country country;

	private Branch branch;

	private int kills;

	private int deaths;

	private int damages;

	private int sorties;

	private int captures;

	private float killDeathRatio;

	private float killDamageRatio;

	public String getCallsign() {
		return callsign;
	}

	public void setCallsign(String callsign) {
		this.callsign = callsign;
	}

	public String getRank() {
		return rank;
	}

	public void setRank(String rank) {
		this.rank = rank;
	}

	public Date getEnlisted() {
		return enlisted;
	}

	public void setEnlisted(Date enlisted) {
		this.enlisted = enlisted;
	}

	public Side getSide() {
		return side;
	}

	public void setSide(Side side) {
		this.side = side;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	public int getKills() {
		return kills;
	}

	public void setKills(int kills) {
		this.kills = kills;
	}

	public int getDeaths() {
		return deaths;
	}

	public void setDeaths(int deaths) {
		this.deaths = deaths;
	}

	public int getDamages() {
		return damages;
	}

	public void setDamages(int damages) {
		this.damages = damages;
	}

	public int getSorties() {
		return sorties;
	}

	public void setSorties(int sorties) {
		this.sorties = sorties;
	}

	public int getCaptures() {
		return captures;
	}

	public void setCaptures(int captures) {
		this.captures = captures;
	}

	public float getKillDeathRatio() {
		return killDeathRatio;
	}

	public void setKillDeathRatio(float killDeathRatio) {
		this.killDeathRatio = killDeathRatio;
	}

	public float getKillDamageRatio() {
		return killDamageRatio;
	}

	public void setKillDamageRatio(float killDamageRatio) {
		this.killDamageRatio = killDamageRatio;
	}

	public static Profile create(Map<String, String> data)
			throws ParseException {
		String callsign = data.get("Callsign");

		if (callsign == null) {
			return null;
		}

		Profile profile = new Profile();

		profile.setCallsign(callsign);
		profile.setRank(data.get("Rank"));
		profile.setSide(Side.valueOf(data.get("Side")));

		String countryText = data.get("Country");

		if (countryText != null) {
			for (Country country : Country.values()) {
				if (countryText.equals(country.getText())) {
					profile.setCountry(country);
					break;
				}
			}
		}

		String branchText = data.get("Branch");

		if (branchText != null) {
			for (Branch branch : Branch.values()) {
				if (branchText.equals(branch.getText())) {
					profile.setBranch(branch);
					break;
				}
			}
		}

		profile.setEnlisted(DATE_FORMAT.parse(data.get("Enlisted")));

		String sorties = data.get("Sorties");
		if (sorties != null) {
			profile.setSorties(NUMBER_FORMAT.parse(sorties).intValue());
		}

		String kills = data.get("Kills");
		if (kills != null) {
			profile.setKills(NUMBER_FORMAT.parse(kills).intValue());
		}

		String deaths = data.get("Deaths");
		if (deaths != null) {
			profile.setDeaths(NUMBER_FORMAT.parse(deaths).intValue());
		}

		String damages = data.get("Damages");
		if (damages != null) {
			profile.setDamages(NUMBER_FORMAT.parse(damages).intValue());
		}

		String captures = data.get("Captures");
		if (captures != null) {
			profile.setCaptures(NUMBER_FORMAT.parse(captures).intValue());
		}

		String killDeath = data.get("Kill/Death");
		if (killDeath != null) {
			profile.setKillDeathRatio(Float.parseFloat(killDeath.replace(',',
					'.')));
		}

		if (profile.getDamages() > 0) {
			profile.setKillDamageRatio((float) profile.getKills()
					/ (float) profile.getDamages());
		}

		return profile;
	}
}
